package com.oalam.bookstore.utils;

import com.oalam.bookstore.domain.BusinessObjectFactory;
import com.oalam.bookstore.domain.accounts.Account;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Administrateur
 */
public  class TestSqlPlik {

    protected Connection conn;

    public TestSqlPlik() {
        try {
            //chargement du driver SQL pour le SGBD MySQL
            Class.forName("com.mysql.jdbc.Driver");

            // recup d'une connection au SGBD
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore", "root", "admin");

        } catch (SQLException ex) {
            Logger.getLogger(TestSqlPlik.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TestSqlPlik.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void finalize() throws Throwable {
        super.finalize();
        try {
            // fermeture des connections
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(TestSqlPlik.class.getName()).log(Level.SEVERE, null, ex);
        }
    }





    private void parseAccount() throws SQLException {
        Statement stmt = conn.createStatement();
        String sql = "SELECT * FROM accounts";
        ResultSet rs = stmt.executeQuery(sql);
        BusinessObjectFactory boFactory = BusinessObjectFactory.getInstance();
        Account a = null;
        // iteration sur le result set
        while (rs.next()) {
            // retrieve record data
            String name = rs.getString("name");
            String email = rs.getString("email");
            String pwd = rs.getString("pwd");
            int id = rs.getInt("id");

            // setup account BO
            a = boFactory.createAccount(email);
            a.setName(name);
            a.setPwd(pwd);


            System.out.println(a.toString());
        }

        rs.close();
        stmt.close();
    }
}
